package view;

import Dao.OriginalDataDao;
import Model.originalData;
import Utils.DBUtils;
import Utils.StringUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

public class originalDataFrm extends JInternalFrame{
    private JPanel panel1;
    private JScrollPane MyPanel;
    private JTable table;
    private JPanel panel3;
    private JButton addButton;
    private JButton resetButton;
    private JButton deleteButton;
    private DBUtils dbUtils=new DBUtils();
    private Dao.OriginalDataDao originalDataDao=new OriginalDataDao();
    public Connection conn;
    public Model.originalData originalData=new originalData();

    originalDataFrm(Connection connection){
        setContentPane(panel1);
        setVisible(true);
        setBounds(50,20,650,500);
        //建立连接
        try {
            this.conn=connection;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
//        MyPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        //初始化表格
        InitialTable();
        //创建监听
        addListen();
    }

    private void addListen() {
        //在表格中点击选中
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                mouseClcPerformed(e);
            }
        });

        //删除
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                delPerformed(e);
            }
        });

        //修改
        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                update(e);
            }
        });

        //添加
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addPerformed(e);
            }
        });
    }

    //添加函数
    private void addPerformed(ActionEvent e) {
        //创建添加界面
        originalData_AddFrm originalData_addFrm=new originalData_AddFrm(conn,this);
        originalData_addFrm.setVisible(true);
//        fillTable(new originalData());
    }

    /**
     * 更新数据函数
     * @param e
     */
    private void update(ActionEvent e) {
        //用户没有选中需要修改的记录的情况
        if(table.getSelectedRow()<0)
        {
            JOptionPane.showMessageDialog(null,"请选择需要修改的记录");
            return;
        }
        //创建修改窗口并将选中记录的信息传入
        originalData_ReviseFrm originalData_ReviseFrm=new originalData_ReviseFrm(conn,originalData,this);
        originalData_ReviseFrm.setVisible(true);
//        fillTable(new originalData());
    }

    //删除函数
    private void delPerformed(ActionEvent e) {
        //获取用户需要删除的记录条数
        int row= table.getSelectedRow();
        String strId=(String)table.getValueAt(row,0);
        //用户没有选中需要删除的记录的情况
        if(StringUtils.isEmpty(strId)){
            JOptionPane.showMessageDialog(null,"请选择要删除的信息");
            return;
        }
        //用户选中需要删除的记录的情况
        try {
            //进行删除
            int numOfaff=originalDataDao.delete(conn,strId);
            //相应系统提示框弹出
            if(numOfaff>0){
                JOptionPane.showMessageDialog(null,"删除成功");
                fillTable(new originalData());
            }
            else {
                JOptionPane.showMessageDialog(null,"删除失败");
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null,"数据库异常");
            throw new RuntimeException(ex);
        }
    }

    //表格点击响应函数
    private void mouseClcPerformed(MouseEvent e) {
        //将当前用户选中的记录中的信息记录
        int row= table.getSelectedRow();
        String currentID=(String)table.getValueAt(row,0);
        int ID=Integer.parseInt(currentID);
        String EgName=(String)table.getValueAt(row,1);
        String CnTitle=(String)table.getValueAt(row,2);
        String UID=(String)table.getValueAt(row,3);
        String Version=(String)table.getValueAt(row,4);
        String KeyWord=(String)table.getValueAt(row,5);
        String Abstract=(String)table.getValueAt(row,6);
        String Content=(String)table.getValueAt(row,7);
        String Sort=(String)table.getValueAt(row,8);
        String Author=(String)table.getValueAt(row,9);
        String Company=(String)table.getValueAt(row,10);
        String Label=(String)table.getValueAt(row,11);
        String Location=(String)table.getValueAt(row,12);
        String Memo=(String) table.getValueAt(row,16);
        String currentCatalog=(String) table.getValueAt(row,17);
        int Catalog=Integer.parseInt(currentCatalog);

        //将相应信息值赋予对象
        originalData.setID(ID);
        originalData.setEgTitle(EgName);
        originalData.setCnTitle(CnTitle);
        originalData.setUID(UID);
        originalData.setVersion(Version);
        originalData.setKeyWord(KeyWord);
        originalData.setAbstract(Abstract);
        originalData.setContent(Content);
        originalData.setSort(Sort);
        originalData.setAuthor(Author);
        originalData.setCompany(Company);
        originalData.setLabel(Label);
        originalData.setLocation(Location);
        originalData.setMemo(Memo);
        originalData.setCatalog(Catalog);
    }

    /**
     * 查询响应函数
     */
//    private void findPerfomed() {
//        String inName=ListNameTxt.getText();
//        Point point=new Point();
//        point.setName(inName);
//        fillTable(point);
//    }

    //初始化表格函数
    private void InitialTable() {
        String []columnName={"ID","EgTitle","CnTitle","Uid","Vision","Keyword","Abstract","Content",
                "Format","Author","Department","Label","Location","AppendTime","CreateTime","UpdateTime","Memo","Catalog"};
        String [][]data={};
        DefaultTableModel defaultTableModel=new DefaultTableModel(data,columnName);
        table.setModel(defaultTableModel);
        fillTable(new originalData());
    }

    //将数据库表中已有的数据获取并填充到表格
    public void fillTable(originalData originalData) {
        DefaultTableModel model= (DefaultTableModel) table.getModel();
        model.setRowCount(0);//清空
        try {
            //获取数据
            ResultSet set =originalDataDao.list(conn,originalData);
            //填充表格
            while (set.next()){
                Vector<String> v=new Vector<String>();
                v.add(set.getString("RAW_INTERNAL_ID"));
                v.add(set.getString("RAW_EN_TITLE"));
                v.add(set.getString("RAW_CN_TITLE"));
                v.add(set.getString("RAW_UUID"));
                v.add(set.getString("RAW_VERSION"));
                v.add(set.getString("RAW_KEYWORDS"));
                v.add(set.getString("RAW_ABSTRACT"));
                v.add(set.getString("RAW_CONTENT"));
                v.add(set.getString("RAW_TYPE"));
                v.add(set.getString("RAW_AUTHORS"));
                v.add(set.getString("RAW_AFFILIATION"));
                v.add(set.getString("RAW_TAGS"));
                v.add(set.getString("RAW_URL"));
                v.add(set.getString("RAW_APPEND_DATE"));
                v.add(set.getString("RAW_UPDATE_DATE"));
                v.add(set.getString("RAW_CREATE_DATE"));
                v.add(set.getString("RAW_MEMO"));
                v.add(set.getString("RAW_CATALOG"));
                model.addRow(v);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,"数据库异常");
            throw new RuntimeException(e);
        }
    }
}
